/*
 * EchoClient.java
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package echoserver;

import java.net.*;
import java.io.*;

/**
 *
 * @author marchese
 */
public class EchoClient {

    /** Creates a new instance of EchoClient */
    public EchoClient() {
    }

    public static void main(String args[]) {
        // arguments supply message and hostname of destination

        Socket s = null;

        try {
            String SERVER = "localhost";
            int serverPort = 8189;
            // s = new Socket(SERVER, serverPort); BLOCCANTE
            s = new Socket();
            s.connect(new InetSocketAddress(SERVER, serverPort), 1000);


            BufferedReader s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter s_out = new PrintWriter(s.getOutputStream(), true /* autoFlush */);

            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

            String data = s_in.readLine();
            System.out.println("Received:" + data);

            while (true) {
                String input = console.readLine();

                s_out.println(input);
                data = s_in.readLine();
                System.out.println("Received:" + data);
            }

        } catch (UnknownHostException e) {
            System.out.println("Sock:" + e.getMessage());
        } catch (EOFException e) {
            System.out.println("EOF:" + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO:" + e.getMessage());
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    System.out.println("close:" + e.getMessage());
                }
            }
        }
    }
}
